<?php

namespace app\modules\api\controllers;

use app\components\ApiController;
use app\models\LoginForm;
use app\models\RegForm;
use app\models\User;
use Yii;

class UserController extends ApiController
{
    public $modelClass = 'app\models\User';

    //注册-测试用
    public function actionReg()
    {
        $model = new RegForm();

        $postData = Yii::$app->request->post();
        // 检查是否有提交的数据
        if (empty($postData)) {
            Yii::$app->response->statusCode = 400;
            return ['message' => '缺少登录数据'];
        }

        $model->setAttributes($postData);

        // 进行表单验证
        if (!$model->validate()) {
            Yii::$app->response->statusCode = 400;
            return ['message' => '输入数据验证失败', 'errors' => $model->errors];
        }

        try {
            $user = $model->reg();
            if ($user) {
                Yii::$app->response->statusCode = 201;
                return ['message' => '注册成功', 'user_id' => $user->id];
            } else {
                Yii::$app->response->statusCode = 500;
                return ['message' => '注册失败，请稍后再试'];
            }
        } catch (\Exception $e) {
            Yii::error('注册过程中出现错误: ' . $e->getMessage(), __METHOD__);
            Yii::$app->response->statusCode = 500;
            return ['message' => '注册过程中出现错误，请稍后再试'];
        }
    }

    // 登录
    public function actionLogin()
    {
        $model    = new LoginForm;
        $postData = Yii::$app->request->post();

        // 检查是否有提交的数据
        if (empty($postData)) {
            Yii::$app->response->statusCode = 400;
            return ['message' => '缺少登录数据'];
        }

        $model->setAttributes($postData);

        // 进行表单验证
        if (!$model->validate()) {
            Yii::$app->response->statusCode = 400;
            return ['message' => '输入数据验证失败', 'errors' => $model->errors];
        }

        try {
            // 根据用户名获取用户
            $user = User::findByUsername($model->username);
            if (!$user) {
                Yii::$app->response->statusCode = 401;
                return ['message' => '用户名不存在'];
            }

            // 验证密码
            if (!$user->validatePassword($model->password, $user->password_hash)) {
                Yii::$app->response->statusCode = 401;
                return ['message' => '密码错误'];
            }

            // 生成 api_token
            $user->generateApiToken();
            if (!$user->save()) {
                throw new \yii\db\Exception('保存用户信息失败');
            }

            // 登录成功，返回 api_token
            Yii::$app->response->statusCode = 200;
            return ['api_token' => $user->api_token];
        } catch (\yii\db\Exception $e) {
            Yii::error('登录过程中出现数据库错误: ' . $e->getMessage(), __METHOD__);
            Yii::$app->response->statusCode = 500;
            return ['message' => '登录过程中出现错误，请稍后再试'];
        }
    }

    //获取用户信息
    public function actionInfo()
    {
        $user = $this->authenticate(Yii::$app->user, Yii::$app->request, Yii::$app->response);
        return $user;
    }

}
